package com.caosy.grassthinkremote.service;

import com.caosy.grassthinkremote.common.BaseContext;
import com.caosy.grassthinkremote.common.GtrConstant;
import com.caosy.grassthinkremote.common.RespData;
import com.caosy.grassthinkremote.entity.Deliver;
import com.caosy.grassthinkremote.entity.DeliverLog;
import com.caosy.grassthinkremote.entity.User;
import com.caosy.grassthinkremote.entity.vo.DeliverReq;
import com.caosy.grassthinkremote.entity.vo.DeliverResp;
import com.caosy.grassthinkremote.mapper.DeliverLogMapper;
import com.caosy.grassthinkremote.mapper.DeliverMapper;
import com.caosy.grassthinkremote.mapper.UserMapper;
import com.caosy.grassthinkremote.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class DeliverService {
    @Autowired
    private DeliverMapper deliverMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BaseUtil baseUtil;
    @Autowired
    private DeliverLogMapper deliverLogMapper;

    /**
     * 添加一条投递记录
     * @param req
     * @return
     */
    public RespData add(DeliverReq req, HttpServletRequest request) {
        Date now = new Date();
        Long userId = baseUtil.getCurUserId(request);

        Deliver deliver = req.toDeliver();
        deliver.setUserId(userId);
        deliver.setCreateTime(now);
        deliver.setUpdateTime(now);
        deliverMapper.insert(deliver);
        return RespData.getSuccess("添加成功!");
    }



    /**
     * 根据条件查询当前用户的投递记录
     * @param req
     * @return
     */
    public RespData list(DeliverReq req, HttpServletRequest request) {
        Long curUserId = baseUtil.getCurUserId(request);
        Deliver deliver = req.toDeliver();
        deliver.setUserId(curUserId);
        List<DeliverResp> delivers = deliverMapper.listByCondition(deliver);
        delivers.stream().forEach(item->{
            item.setStatusDesc(Deliver.DeliverStatusEnum.statusToDesc.get(item.getStatus()));
        });
        return RespData.getSuccess(delivers);
    }

    /**
     * 推进对应的投递记录的流程
     * @param req
     * @return
     */
    public RespData push(DeliverReq req) {
        Date now = new Date();
        //参数判空
        if (req.getId() == null) {
            return RespData.getFailure("传入记录id不可为空！");
        }
        //查询出对应的记录
        Deliver deliver = deliverMapper.selectById(req.getId());
        //判断id是否无记录
        if (Objects.isNull(deliver)) return RespData.getFailure("无这条记录！");
        //判断是否不可再推进
        if (deliver.getStatus().equals(Deliver.DeliverStatusEnum.OFFER.status)
                || deliver.getStatus().equals(Deliver.DeliverStatusEnum.PASS.status))
            return RespData.getFailure("不可再推进！");

        //可推进，则推进，推进同时记录log
        int originalStatus = deliver.getStatus();
        int currentStatus = originalStatus + 1;
        deliver.setStatus(currentStatus);
        deliver.setUpdateTime(now);
        deliverMapper.updateById(deliver);

        addDeliverLog(now, deliver, originalStatus, currentStatus);

        return RespData.getSuccess("成功！");
    }

    public void addDeliverLog(Date now, Deliver deliver, int originalStatus, int currentStatus) {
        DeliverLog deliverLog = DeliverLog.builder()
                .deliverId(deliver.getId())
                .originalStatus(originalStatus)
                .currentStatus(currentStatus)
                .updateTime(now)
                .build();
        deliverLogMapper.insert(deliverLog);
    }

    /**
     * 挂掉某个投递
     * @param req
     * @return
     */
    public RespData pass(DeliverReq req) {
        Date now = new Date();
        //参数判空
        if (req.getId() == null) {
            return RespData.getFailure("传入记录id不可为空！");
        }
        //查询出对应的记录
        Deliver deliver = deliverMapper.selectById(req.getId());
        //判断id是否无记录
        if (Objects.isNull(deliver)) return RespData.getFailure("无这条记录！");
        //判断是否不可再推进
        if (deliver.getStatus().equals(Deliver.DeliverStatusEnum.OFFER.status)
                || deliver.getStatus().equals(Deliver.DeliverStatusEnum.PASS.status))
            return RespData.getFailure("不可再推进！");

        //可推进，则推进，推进同时记录log
        int originalStatus = deliver.getStatus();
        deliver.setStatus(Deliver.DeliverStatusEnum.PASS.status);
        deliver.setUpdateTime(now);
        deliverMapper.updateById(deliver);

        addDeliverLog(now,deliver,originalStatus,Deliver.DeliverStatusEnum.PASS.status);

        return RespData.getSuccess("操作成功！");
    }
}
